<?php

    if (!defined('BASEPATH'))
        exit('No direct script access allowed');

    class Transfers extends MY_Controller {

        protected $data;

        public function __construct() {
            parent::__construct();

            // Load the rest client spark
            $this->load->spark('restclient/2.1.0');

            // Run some setup
            $this->rest->initialize(array(
                'server' => REST_URL_HOTELS,
                'http_user' => 'cipa',
                'http_pass' => 'booking',
                'http_auth' => 'basic' // or 'digest' 
            ));

            $this->activeLang = $this->lang->mci_current();

            require_once EXTERNAL_URL . 'urlify.php';

            // Do caching
            $this->load->driver('cache', array('adapter' => 'file')); // za uklucuvanje na kes mogucnosti
        }

        public function index() {

            $rest_data = array(
                'lang' => $this->activeLang
            );

            $file = cache_filename($rest_data, 'transfers_dest_from');

            if (!$this->data['destFrom'] = $this->cache->get($file)) {

                $this->data['destFrom'] = $this->rest->post('transfersDestFrom', $rest_data);

                if (DO_CACHE)
                    $this->cache->save($file, $this->data['destFrom'], 300);
            }

            /* -----------------------------------------------------------------------
              Load transfer page data
              ----------------------------------------------------------------------- */
            $this->data['page'] = $this->findPost(23);

            $this->data['seo_title'] = lang('title-transfers');
            $this->data['seo_description'] = lang('description-transfers');
            $this->data['seo_keywords'] = '';
            $this->template->load('transfers/home', $this->data);
        }

        public function findDestTo($city_ID) {

            $rest_data = array(
                'lang' => $this->activeLang,
                'city_ID' => $city_ID
            );

            $file = cache_filename($rest_data, 'transfers_dest_to');

            if (!$this->data['destTo'] = $this->cache->get($file)) {

                $this->data['destTo'] = $this->rest->post('transfersDestTo', $rest_data);

                if (DO_CACHE)
                    $this->cache->save($file, $this->data['destTo'], 300);
            }

            $this->firephp->log($this->data);

            echo json_encode(isset($this->data['destTo']) ? $this->data['destTo'] : array());
        }

        public function search() {

            $rest_data = $_GET;
            $rest_data['lang'] = $this->activeLang;

//            $this->firephp->log($rest_data);

            $file = cache_filename($rest_data, 'transfers_list');

            if (!$this->data['results'] = $this->cache->get($file)) {

                $this->data['results'] = $this->rest->post('searchTransfers', $rest_data);

                if (DO_CACHE)
                    $this->cache->save($file, $this->data['results'], 300);
            }

//            $this->firephp->log($this->data);

            $this->data['destFrom'] = isset($this->data['results']->destFrom) ? $this->data['results']->destFrom : NULL;
            $this->data['destTo'] = isset($this->data['results']->destTo) ? $this->data['results']->destTo : NULL;
            $this->data['results'] = isset($this->data['results']->transfers) ? $this->data['results']->transfers : NULL;

//            $this->firephp->log($this->data);

            $this->data['seo_title'] = lang('title-transfers');
            $this->data['seo_description'] = lang('description-transfers');
            $this->data['seo_keywords'] = '';

            $this->template->load('transfers/search', $this->data);
        }

        public function reservation() {

            $this->load->helper('timemaker');
            $this->load->helper('percent');

            $rest_data = $_GET;
            $rest_data['lang'] = $this->activeLang;

            $file = cache_filename($rest_data, 'transfers_list');

            if (!$this->data['results'] = $this->cache->get($file)) {

                $this->data['results'] = $this->rest->post('searchTransfers', $rest_data);

                if (DO_CACHE)
                    $this->cache->save($file, $this->data['results'], 300);
            }

            foreach ($this->data['results']->transfers as $transfer)
                if ($transfer->ID == $_GET['transfer_id'])
                    $this->data['reservation'] = $transfer;

            $this->firephp->log($this->data['reservation']);

            $this->data['seo_title'] = lang('title-reservation_details');
            $this->data['seo_description'] = "";
            $this->data['seo_keywords'] = "";

            $cookie = array(
                'name' => 'cipa_cookie',
                'value' => '1',
                'expire' => '86500'
            );

            $this->input->set_cookie($cookie); // Set coockie for redirecting on confirmation page reload

            $this->load->config('countries');
            $this->template->set_view('template_checkout');
            $this->template->load('site/checkoutTransfers', $this->data);
        }

    }
    